@model WorkflowDefinitionListViewModel
@{
    ViewBag.Title = "Workflows";

    var deleteUrlTemplate = Url.Action("Delete", new { id = "id" }).Replace("id", "${id}");
}

@section FootScripts
{
    <script type="text/javascript">
        const form = document.querySelector('#listForm');
        
        function onDeleteClick(e, id){
            e.preventDefault();
            
            if(!confirm('Are you sure you want to delete this workflow?'))
                return;
            
            form.action = `@Html.Raw(deleteUrlTemplate)`;
            form.submit();
        }
    </script>
}

<div class="container-fluid mt--7">
    <div class="row">
        <div class="col">
            <div class="card shadow">
                <div class="card-header bg-white border-0">
                    <div class="row align-items-center">
                        <div class="col-8">
                            <h3 class="mb-0">Workflow Definitions</h3>
                        </div>
                        <div class="col-4 text-right">
                            <a asp-action="Create" class="btn btn-primary">Create Workflow</a>
                        </div>
                    </div>
                </div>
                <div class="table-responsive">
                    <form id="listForm" method="post" class="table-wrapper">
                        <table class="table align-items-center table-flush">
                            <thead class="thead-light">
                            <tr>
                                <th scope="col">Name</th>
                                <th scope="col">Description</th>
                                <th scope="col">Instances</th>
                                <th scope="col">Latest Version</th>
                                <th scope="col">Published Version</th>
                                <th scope="col">Enabled</th>
                                <th scope="col"></th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach (var grouping in Model.WorkflowDefinitions)
                            {
                                var latest = grouping.First(x => x.WorkflowDefinition.IsLatest);
                                var published = grouping.FirstOrDefault(x => x.WorkflowDefinition.IsPublished);
                                var publishedVersion = published?.WorkflowDefinition.Version.ToString() ?? "-";
                                <tr>
                                    <th scope="row">
                                        <div class="media align-items-center">
                                            <div class="media-body">
                                                <a asp-action="Edit" asp-route-id="@latest.WorkflowDefinition.DefinitionId" class="mb-0 text-sm">@latest.WorkflowDefinition.Name</a>
                                            </div>
                                        </div>
                                    </th>
                                    <td>@latest.WorkflowDefinition.Description</td>
                                    <td>
                                        <ul class="list-unstyled">
                                            @if (latest.ExecutingCount > 0)
                                            {
                                                <li>
                                                    <span class="badge badge-dot mr-4">
                                                        <i class="bg-info"></i>
                                                        <a
                                                            asp-action="Index"
                                                            asp-controller="WorkflowInstance"
                                                            asp-route-definitionId="@latest.WorkflowDefinition.DefinitionId"
                                                            asp-route-status="Executing">
                                                            @($"{latest.ExecutingCount} Executing")
                                                        </a>
                                                    </span>
                                                </li>
                                            }
                                            @if (latest.FaultedCount > 0)
                                            {
                                                <li>
                                                    <span class="badge badge-dot mr-4">
                                                        <i class="bg-warning"></i>
                                                        <a
                                                            asp-action="Index"
                                                            asp-controller="WorkflowInstance"
                                                            asp-route-definitionId="@latest.WorkflowDefinition.DefinitionId"
                                                            asp-route-status="Faulted">
                                                            @($"{latest.FaultedCount} Faulted")
                                                        </a>
                                                    </span>
                                                </li>
                                            }
                                            @if (latest.FinishedCount > 0)
                                            {
                                                <li>
                                                    <span class="badge badge-dot mr-4">
                                                        <i class="bg-success"></i>
                                                        <a
                                                            asp-action="Index"
                                                            asp-controller="WorkflowInstance"
                                                            asp-route-definitionId="@latest.WorkflowDefinition.DefinitionId"
                                                            asp-route-status="Finished">
                                                            @($"{latest.FinishedCount} Finished")
                                                        </a>
                                                    </span>
                                                </li>
                                            }
                                            @if (latest.AbortedCount > 0)
                                            {
                                                <li>
                                                    <span class="badge badge-dot mr-4">
                                                        <i class="bg-default"></i>
                                                        <a
                                                            asp-action="Index"
                                                            asp-controller="WorkflowInstance"
                                                            asp-route-definitionId="@latest.WorkflowDefinition.DefinitionId"
                                                            asp-route-status="Aborted">
                                                            @($"{latest.AbortedCount} Aborted")
                                                        </a>
                                                    </span>
                                                </li>
                                            }
                                        </ul>
                                    </td>
                                    <td>
                                        @latest.WorkflowDefinition.Version
                                    </td>
                                    <td>
                                        @publishedVersion
                                    </td>
                                    <td>
                                        <label class="custom-toggle disabled">
                                            <input type="checkbox" @if(!latest.WorkflowDefinition.IsDisabled){<text>checked</text>} disabled="disabled">
                                            <span class="custom-toggle-slider rounded-circle"></span>
                                        </label>
                                    </td>
                                    <td class="text-right">
                                        <div class="dropdown">
                                            <a class="btn btn-sm btn-icon-only text-light" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                                <i class="fas fa-ellipsis-v"></i>
                                            </a>
                                            <div class="dropdown-menu dropdown-menu-right dropdown-menu-arrow">
                                                <a class="dropdown-item" asp-action="Edit" asp-route-id="@latest.WorkflowDefinition.DefinitionId">Edit</a>
                                                <a class="dropdown-item" href="#">Publish Draft</a>
                                                <a class="dropdown-item" href="#">Unpublish</a>
                                                <a class="dropdown-item" href="#">History</a>
                                                <a class="dropdown-item" href="#" onclick="onDeleteClick(event, '@latest.WorkflowDefinition.DefinitionId')">Delete</a>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            }
                            </tbody>
                        </table>
                    </form>
                </div>
                <div class="card-footer py-4">
                    <nav aria-label="...">
                        <ul class="pagination justify-content-end mb-0">
                            <li class="page-item disabled">
                                <a class="page-link" href="#" tabindex="-1">
                                    <i class="fas fa-angle-left"></i>
                                    <span class="sr-only">Previous</span>
                                </a>
                            </li>
                            <li class="page-item active">
                                <a class="page-link" href="#">1</a>
                            </li>
                            <li class="page-item">
                                <a class="page-link" href="#">2 <span class="sr-only">(current)</span></a>
                            </li>
                            <li class="page-item">
                                <a class="page-link" href="#">3</a>
                            </li>
                            <li class="page-item">
                                <a class="page-link" href="#">
                                    <i class="fas fa-angle-right"></i>
                                    <span class="sr-only">Next</span>
                                </a>
                            </li>
                        </ul>
                    </nav>
                </div>
            </div>
        </div>
    </div>
    @await Html.PartialAsync("Footer")
</div>